﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FortuneHeaven.Admin.Common;
using FortuneHeavenBAL;
using FortuneHeavenCommon.DataMap;
using FortuneHeavenCommon.Enum;

namespace FortuneHeaven.Admin.Pages.Shop
{
    public partial class GiftDetail : FortuneHeaven.Admin.Common.PageBase
    {
        public override string PageID
        {
            get { return "F23"; }
        }
        private string pre = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            {
                dbType.Items.Clear();
                foreach (GiftType type in Enum.GetValues(typeof(GiftType)))
                {
                    dbType.Items.Add(new ListItem(AttributeHelper.GetDescription(type), ((int)type).ToString()));
                }
                if (ID >= 0)
                {
                    GiftBO bo = new GiftBO();
                    Gift g = bo.GetInfo(ID);
                    this.txtName.Text = g.Name;
                    this.clOverDueTime.SelectedDate = g.OverDue;

                    this.txtPrice.Text = g.Price.ToString();

                    this.txtDescription.Text = g.Description;
                    this.dbType.SelectedValue = g.Type.ToString();
                }
            }
        }

        public new int? ID
        {
            get
            {
                return string.IsNullOrEmpty(Request["ID"]) ? -1 : int.Parse(Request["ID"]);
            }
        }

        public string Pre
        {
            get
            {
                if (!string.IsNullOrEmpty(pre))
                    return pre;
                else
                    pre = Server.UrlDecode(Request["pre"]);
                return pre;
            }
        }

        public string CheckStatus()
        {
            if (ID<0)
            {
                return "新增奖品";
            }
            else
            {
                return "修改奖品";
            }
        }
        public void Save(object sender, EventArgs arg)
        {
            if (!this.IsValid)
            {
                return;
            }
        
            GiftBO bo = new GiftBO();
            Gift g;
            if (ID >= 0)
            {
                g = bo.GetInfo(ID);

            }
            else
            {
                g = new Gift();
                  g.SubmitTime = System.DateTime.Now;
            }
            g.Name = this.txtName.Text;
            g.OverDue = this.clOverDueTime.SelectedDate;
          
            g.Price = int.Parse( this.txtPrice.Text);

            g.Description = this.txtDescription.Text;
            g.Type = int.Parse( this.dbType.SelectedValue);

            if (HttpContext.Current.Request.Files.Count > 0 &&! string.IsNullOrEmpty( HttpContext.Current.Request.Files[0].FileName))
            {
                string name = "";
                string error = "";
                bool suc = ImagesManager.SaveGiftImg(HttpContext.Current, ref name, ref error);
                if (!suc)
                {
                    ShowCurError(error);
                    return;
                }
                else
                {
                    g.Image = name;
                }
            }
            if (ID >= 0)
            {
                bo.Save(g, false);
                Response.Write(string.Format("<Html> <script>alert('修改成功');document.location ='{0}'; </script> </HTML>", Pre));

            }
            else
            {
                g.ID =  bo.GetMaxID();
                bo.Save(g, true);
                Response.Write(string.Format("<Html> <script>alert('新增成功');document.location ='{0}'; </script> </HTML>", Pre));
                Response.Flush();
            }
        }
        public void CancelSave(object sender, EventArgs arg)
        {
            Response.Redirect(Pre);
        }
    }
}